package com.ruoyi.web.controller.used;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.framework.util.MD5Util;
import com.ruoyi.framework.util.RandomUtil;
import com.ruoyi.used.domain.UsedMember;
import com.ruoyi.used.domain.UsedUser;
import com.ruoyi.used.service.IUsedMemberService;
import com.ruoyi.used.service.IUsedUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2020-07-09
 */
@Controller
@RequestMapping("/used/user")
public class UsedUserController extends BaseController
{
    private String prefix = "used/user";

    @Autowired
    private IUsedUserService usedUserService;

    @Autowired
    private IUsedMemberService memberService;

    @RequiresPermissions("used:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询用户列表
     */
    @RequiresPermissions("used:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UsedUser usedUser)
    {
        startPage();
        List<UsedUser> list = usedUserService.selectUsedUserList(usedUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("used:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UsedUser usedUser)
    {
        List<UsedUser> list = usedUserService.selectUsedUserList(usedUser);
        ExcelUtil<UsedUser> util = new ExcelUtil<UsedUser>(UsedUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap map, UsedMember member)
    {
        //下拉框添加会员等级
        List<UsedMember> list = memberService.selectUsedMemberList(member);
        map.put("member",list);

        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("used:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UsedUser usedUser)
    {
//        判断手机号是否存在
        UsedUser result = usedUserService.selectUserByPhone(usedUser.getPhone());
        //判断邀请码是否存在
        UsedUser user = usedUserService.selectCode(usedUser.getInviteCode());
        if (result != null){
            return error("手机号已存在");
        }
        if (user!=null){
            System.out.println(user);
            return  error("邀请码已存在");
        }
        else if (result == null&&user == null){
            System.out.println(usedUser);
            usedUser.setPassword(MD5Util.encode(usedUser.getPassword()));   //密码加密
            usedUser.setInviteCode(RandomUtil.generateRandomCharAndNumber(6));  //生成随机邀请码
            return toAjax(usedUserService.insertUsedUser(usedUser));
        }
        return success();
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{uid}")
    public String edit(@PathVariable("uid") Long uid, ModelMap mmap,UsedMember member)
    {
        UsedUser usedUser = usedUserService.selectUsedUserById(uid);
        mmap.put("usedUser", usedUser);

        //修改时的下拉框
        List<UsedMember> list = memberService.selectUsedMemberList(member);
        mmap.put("member",list);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("used:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UsedUser usedUser)
    {
            return toAjax(usedUserService.updateUsedUser(usedUser));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("used:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(usedUserService.deleteUsedUserByIds(ids));
    }

}
